//$CC test_noPDO.c -o ectest_noPDO -L /home/lee/igh-mc -lethercat
//scp ectest_noPDO root@192.168.58.1:/home/mc
#include "config_qn.h"

void check_master_state(void)
{
    ec_master_state_t ms;

    ecrt_master_state(master, &ms);

    if (ms.slaves_responding != master_state.slaves_responding) {
        printf("found %u slave(s).\n", ms.slaves_responding);
    }
    if (ms.al_states != master_state.al_states) {
        printf("AL states: 0x%02X.\n", ms.al_states);
    }
    if (ms.link_up != master_state.link_up) {
        printf("Link is %s.\n", ms.link_up ? "up" : "down");
    }

    master_state = ms;
}

/*****************************************************************************/

void check_slave0_config_states(void)
{
    ec_slave_config_state_t s;

    ecrt_slave_config_state(sc_cooldrive0, &s);

    if (s.al_state != sc_cooldrive0_state.al_state) {
        printf("CoolDrive0: State 0x%02X.\n", s.al_state);
    }
    if (s.online != sc_cooldrive0_state.online) {
        printf("CoolDrive0: %s.\n", s.online ? "online" : "offline");
    }
    if (s.operational != sc_cooldrive0_state.operational) {
        printf("CoolDrive0: %soperational.\n", s.operational ? "" : "Not ");
    }

    sc_cooldrive0_state = s;
}
/*****************************************************************************/

void check_slave1_config_states(void)
{
    ec_slave_config_state_t s;

    ecrt_slave_config_state(sc_cooldrive1, &s);

    if (s.al_state != sc_cooldrive1_state.al_state) {
        printf("CoolDrive1: State 0x%02X.\n", s.al_state);
    }
    if (s.online != sc_cooldrive1_state.online) {
        printf("CoolDrive1: %s.\n", s.online ? "online" : "offline");
    }
    if (s.operational != sc_cooldrive1_state.operational) {
        printf("CoolDrive1: %soperational.\n", s.operational ? "" : "Not ");
    }

    sc_cooldrive1_state = s;
}
/*****************************************************************************/

void check_domain1_state(void)
{
    ec_domain_state_t ds;

    ecrt_domain_state(domainServoInput, &ds);

    if (ds.working_counter != domainServoInput_state.working_counter) {
        printf("Domain1: WC %u.\n", ds.working_counter);
    }
    if (ds.wc_state != domainServoInput_state.wc_state) {
        printf("Domain1: State %u.\n", ds.wc_state);
    }

    domainServoInput_state = ds;
}

/*****************************************************************************/


int main(void)
{
    
    master = ecrt_request_master(0);
    if (!master) {
        return -1;
    }
    printf("request_master sucess\n");
    check_master_state();
    
    printf("master get slave config\n");
    sc_cooldrive0=ecrt_master_slave_config(master,CoolDrivePos0,CoolDrive); 
    sc_cooldrive1=ecrt_master_slave_config(master,CoolDrivePos1,CoolDrive);       
    //check_slave_config_states();
    printf("master_reset,Retry configuring slaves.\n");
    ecrt_master_reset(master); 
    check_slave0_config_states();
    check_slave1_config_states();
    pause();
    printf("release_master\n");
    ecrt_release_master(master);
    
    return 0;
}



